package controller;

import com.google.gson.Gson;
import model.Meetingmanage;
import services.MeetingmanageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user1")//访问接口的名字
public class MeetingmanageController extends HttpServlet {
    private static Gson gson=new Gson();//第三方插件，返回gson格式的插件

    @Override//重写
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//请求 响应  针对的是前端页面的get方法
        //用户所做的操作
        resp.setContentType("text/html;charset=utf-8");
        String oper=req.getParameter("oper");//获取地址栏当中oper后边的值   请求对象实例化
        System.out.println("获取地址栏当中的数据为："+oper);//能否获取地址栏中的值
        //meetingmanage表的数据
        /*String title=req.getParameter("title");
        String creater=req.getParameter("creater");
        String type=req.getParameter("type");
        String maintainmain=req.getParameter("maintainmain");
        String place=req.getParameter("place");
        String participant=req.getParameter("participant");
        String accessory=req.getParameter("accessory");*/

        Meetingmanage meetingmanage=new Meetingmanage();////meetingmanage表实例化

        /*meetingmanage.setTitle(title);
        meetingmanage.setCreater(creater);//调用set方法拿到给创建人设置的值
        meetingmanage.setType(type);
        meetingmanage.setMaintainmain(maintainmain);
        meetingmanage.setPlace(place);
        meetingmanage.setParticipant(participant);
        meetingmanage.setAccessory(accessory);*/


        List<Meetingmanage> tempReturn1 = null;//返回集合操作结果  （查询时候使用）没有new的还要进行实例化



        /*System.out.println("获取地址栏当中的数据为："+creater);*/
        int tempReturn=0;//返回int操作结果  （增删改时使用）
        MeetingmanageService meetingmanageService= new MeetingmanageService();//实例化业务功能类
        System.out.println("您要做的操作是（1：单项查询/2:增加/3:修改/4:删除/）" + oper);
        if(oper!=null){//判断地址栏输入的oper不等于空    选择要做的操作
            if (req.getParameter("meetingmanageid") != null) {
                String meetingmanageid = req.getParameter("meetingmanageid");
                System.out.println("获得地址栏中表的数据为" + meetingmanageid);
            }
            if (req.getParameter("title") != null) {
                String title = req.getParameter("title");
                System.out.println("获得地址栏中表的数据为" + title);
                meetingmanage.setTitle(title);
            }
            if (req.getParameter("maintaintime") != null) {
                String maintaintime = req.getParameter("maintaintime");
                System.out.println("获得地址栏中表的数据为" + maintaintime);
                meetingmanage.setMaintaintime(maintaintime);
            }
            if (req.getParameter("createtime") != null) {
                String createtime = req.getParameter("createtime");
                System.out.println("获得地址栏中表的数据为" + createtime);
                meetingmanage.setCreatetime(createtime);
            }

            if (req.getParameter("creater") != null) {
                String creater = req.getParameter("creater");
                System.out.println("获得地址栏中表的数据为" + creater);
                meetingmanage.setCreater(creater);
            }
            if (req.getParameter("type") != null) {
                String type = req.getParameter("type");
                System.out.println("获得地址栏中表的数据为" + type);
                meetingmanage.setType(type);
            }
            if (req.getParameter("maintainmain") != null) {
                String maintainmain = req.getParameter("maintainmain");
                System.out.println("获得地址栏中表的数据为" + maintainmain);
                meetingmanage.setMaintainmain(maintainmain);
            }
            if (req.getParameter("place") != null) {
                String place = req.getParameter("place");
                System.out.println("获得地址栏中表的数据为" + place);
                meetingmanage.setPlace(place);
            }
            if (req.getParameter("participant") != null) {
                String participant = req.getParameter("participant");
                System.out.println("获得地址栏中表的数据为" + participant);
                meetingmanage.setParticipant(participant);
            }
            if (req.getParameter("accessory") != null) {
                String accessory = req.getParameter("accessory");
                System.out.println("获得地址栏中表的数据为" + accessory);
                meetingmanage.setAccessory(accessory);
            }


            //操作的内容
            switch (oper){
                case "1":
                    //查询
                    tempReturn1=meetingmanageService.getDan(meetingmanage);//调用service层的getall方法 返回查询结果
                    break;
                case "2":
                    //添加
                    tempReturn=meetingmanageService.add(meetingmanage);//调用service层的add方法  返回添加结果
                    if (tempReturn == 0) {
                        System.out.println("失败" + tempReturn);
                    } else {
                        System.out.println("成功" + tempReturn);
                    }
                    break;
                case "3":
                    //修改
                    String meetingmanageid=req.getParameter("meetingmanageid");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    meetingmanage.setMeetingmanageid(Integer.parseInt(meetingmanageid));//强制类型转换  给实体主键赋值
                    System.out.println(meetingmanageid);
                    String creater = req.getParameter("creater");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    meetingmanage.setCreater(creater);
                    System.out.println(creater);
                    tempReturn=meetingmanageService.update(meetingmanage);//调用service层的update方法  返回修改结果
                    break;
                case "4":
                    //删除
                    String meetingmanageid1=req.getParameter("meetingmanageid");//主键  删除时必须获取表中主键（地址栏中必须有该参数）
                    if(meetingmanageid1!=null){
                        meetingmanage.setMeetingmanageid(Integer.parseInt(meetingmanageid1));//强制类型转换  给实体主键赋值
                        System.out.println(meetingmanageid1);
                        tempReturn=meetingmanageService.del(meetingmanage);//调用service层的del方法  返回删除结果

                    }
                    break;
                case "5":
                    //查询全部
                    tempReturn1 = meetingmanageService.getAll(meetingmanage);//调用service层的getall方法 返回查询结果
                    break;
                case "6":
                    //更改方法按id查询
                    String meetingmanageid2 = req.getParameter("meetingmanageid");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
                    meetingmanage.setMeetingmanageid(Integer.parseInt(meetingmanageid2));//强制类型转换  给实体主键赋值
                    tempReturn1 = meetingmanageService.getAddc(meetingmanage);//调用service层的getADDC方法 返回查询结果
                    break;

        }
        //设置返回信息

        //将所有的返回值都写入到map当中
        Map map=new HashMap();//
        map.put("code","1");//返回编号：1：成功；0：失败；-1：项目负责人重复
        map.put("statue","success");
        if(tempReturn1!=null) {
            map.put("results", tempReturn1);//成功的结果 操作的结果
        }else {
            map.put("results", tempReturn);
        }
        //将数据转换为gson格式进行发送
        String rs=gson.toJson(map);
        //将数据发送后并进行打印
        resp.getWriter().print(rs);
         }
    }

    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{//请求  响应  针对的是前端页面的post方法
        super.doGet(req,resp);//调用父类方法
    }
}
